package com.weitu.permission.roleAuthority.service.impl;

import com.weitu.permission.role.entity.RoleAuthority;
import com.weitu.permission.roleAuthority.mapper.RoleAuthorityMapper;
import com.weitu.permission.roleAuthority.service.RoleAuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author LiMeng 2017/3/21
 */
@Service
public class RoleAuthorityServiceImpl implements RoleAuthorityService {

    private final RoleAuthorityMapper roleAuthorityMapper;

    @Autowired
    public RoleAuthorityServiceImpl(RoleAuthorityMapper roleAuthorityMapper) {
        this.roleAuthorityMapper = roleAuthorityMapper;
    }

    @Override
    @Transactional
    public boolean deleteRoleAuthorityByRoleId(String roleId) {
        int count = roleAuthorityMapper.deleteRoleAuthorityByRoleId(roleId);
        return count > 0;
    }

    @Override
    @Transactional
    public boolean insertRoleAuthority(List<RoleAuthority> roleAuthorityList) {
        int count = roleAuthorityMapper.insertRoleAuthority(roleAuthorityList);
        return count == roleAuthorityList.size();
    }

    @Override
    @Transactional
    public boolean updateRoleAuthority(String roleId, List<RoleAuthority> roleAuthorityList) {
        roleAuthorityMapper.deleteRoleAuthorityByRoleId(roleId);
        int j = roleAuthorityMapper.insertRoleAuthority(roleAuthorityList);
        return j == roleAuthorityList.size();
    }
}
